import java.util.concurrent.TimeUnit;

/**
 * @author wangC
 * @create 2020-06-16 下午10:51
 * @Description
 **/
public class StartThread {

    //什么是进程、线程
    //进程就是一个程序运行起来的状态，线程是一个进程中的不同的执行路径
    //进程是OS分配资源的基本单位，线程是执行调度的基本单位
    private static class T1 extends Thread {
        @Override
        public void run() {
            for(int i=0; i<10; i++) {
                try {
                    TimeUnit.MICROSECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("T1");
            }
        }
    }


    //创建线程
    static class MyThread extends Thread {
        @Override
        public void run() {
            System.out.println("Hello MyThread!");
        }
    }

    static class MyRun implements Runnable {

        public void run() {
            System.out.println("Hello MyRun!");
        }
    }



    /**
     * 线程睡眠等待，返回就绪状态
     */
    static void testSleep() {
        new Thread(()->{
            for(int i=0; i<100; i++) {
                System.out.println("A" + i);
                try {
                    Thread.sleep(500);
                    //TimeUnit.Milliseconds.sleep(500)
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    /**
     * thread yield
     * 线程让出cpu资源,进入等待队列，返回就绪状态，根据CPU调度方法选出下个执行的线程，有可能是自己继续被选中
     */
    static void testYield() {
        new Thread(()->{
            for(int i=0; i<100; i++) {
                System.out.println("A" + i);
                if(i%10 == 0) Thread.yield();


            }
        }).start();

        new Thread(()->{
            for(int i=0; i<100; i++) {
                System.out.println("------------B" + i);
                if(i%10 == 0) Thread.yield();
            }
        }).start();
    }

    /**
     * thread join
     * ti代码块中执行t2.join，t1停止，等待t2执行完再回来执行t1
     */
    static void testJoin() {
        Thread t1 = new Thread(()->{
            for(int i=0; i<100; i++) {
                System.out.println("A" + i);
                try {
                    Thread.sleep(500);
                    //TimeUnit.Milliseconds.sleep(500)
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread t2 = new Thread(()->{

            try {
                t1.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            for(int i=0; i<100; i++) {
                System.out.println("A" + i);
                try {
                    Thread.sleep(500);
                    //TimeUnit.Milliseconds.sleep(500)
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        t1.start();
        t2.start();
    }

    //ThreadState  getState()

    /**
     *  thread state
     */
    static class MyThreadState extends Thread {
        @Override
        public void run() {
            System.out.println(this.getState());

            for(int i=0; i<10; i++) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println(i);
            }
        }
    }

    public static void main(String[] args) {
        //new T1().run();   //直接调用T1里面的run方法，只有一条执行路径
        new T1().start();   //是启动另一条执行路径
        for(int i=0; i<10; i++) {
            try {
                TimeUnit.MICROSECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("main");
        }

        //创建线程
        //启动线程的三种方式：继承Thread  实现Runnable   线程池 Excutors.newCachedThrad
        new MyThread().start();
        new Thread(new MyRun()).start();
        new Thread(()->{
            System.out.println("Hello Lambda!");
        }).start();

        //sleep yield join
        testSleep();//线程睡眠等待，返回就绪状态
        testYield();//线程让出cpu资源,进入等待队列，返回就绪状态，根据CPU调度方法选出下个执行的线程，有可能是自己继续被选中
        testJoin();// ti代码块中执行t2.join，t1停止，等待t2执行完再回来执行t1

        //ThreadState  线程状态
        Thread t = new MyThreadState();
        System.out.println(t.getState());
        t.start();
        try {
            t.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(t.getState());
    }




}
